package com.woniu.order.controller;

import com.woniu.entity.Result;
import com.woniu.order.entity.dto.DisplayChooseProductByUserDTO;
import com.woniu.order.pojo.OrderItem;
import com.woniu.order.service.IOrderItemService;
import com.woniu.util.JwtUtil;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单中所包含的商品 前端控制器
 * </p>
 *
 * @author lydia
 * @since 2023-02-16
 */
@RestController
@RequestMapping("/orderItem")
@Api(tags = "订单详情管理（购物车）")
public class OrderItemController {

    @Autowired
    private IOrderItemService orderItemService;


    @GetMapping("/{id}")
    @ApiOperation("根据商品id查询购物车内的商品信息")
    public Result<OrderItem> queryById(@PathVariable String id){
        OrderItem orderItem = orderItemService.getById(id);
        return Result.ok(orderItem);
    }

    @PostMapping("/cart/add/{id}/{productQuantity}/{username}")
    @ApiOperation("添加商品到购物车")
    public Result addProductToCart(@PathVariable String id, @PathVariable Integer productQuantity, @PathVariable String username) throws Exception {
        //解析token
//        String token = request.getHeader("Authorization");
//        Claims claims = JwtUtil.parseJWT(token);
//        String username = (String) claims.get("username").toString();

        //接收响应，sevice处理
        orderItemService.addProductToCart(id,productQuantity,username);
        return Result.ok();
    }

    @GetMapping("/cart/{username}")
    @ApiOperation("查询购物车列表")
    public Result displayProductFormCart(@PathVariable String username) throws Exception {
        //解析token
        //String token = request.getHeader("Authorization");
        //Claims claims = JwtUtil.parseJWT(token);
        //String username = (String) claims.get("username");
        List<OrderItem> list = orderItemService.displayProductFormCart(username);
        return Result.ok(list);
    }

    @DeleteMapping("/cart/add/{id}/{productQuantity}/{username}")
    @ApiOperation("删除商品")
    public Result deleteProductToCart(@PathVariable String username, @PathVariable String id, @PathVariable Integer productQuantity ) throws Exception {
        //解析token
//        String token = request.getHeader("Authorization");
//        Claims claims = JwtUtil.parseJWT(token);
//        String username = (String) claims.get("username");
        orderItemService.deleteProductToCart(id,username,productQuantity);
        return Result.ok();
    }

    // 跳转到结算页面
    @GetMapping("/cart/list/{username}")
    public Result<List<OrderItem>> listSelectCartItem(String[] ids, @PathVariable String username) throws Exception {
        // 1. 通过token获取用户名
        //String token = request.getHeader("Authorization");
        //Claims claims = JwtUtil.parseJWT(token);
        //String username = claims.get("username").toString();
        // 2. 调用service获取购物车中的商品
        List<OrderItem> orderItemList = orderItemService.listSelectCartItem(ids, username);
        // 3，返回数据
        return Result.ok(orderItemList);
    }
   // @PostMapping("/cart/list")
   // @ApiOperation("展示下单列表+完成下单")
   // public Result displayChooseProductsByUser(@RequestBody DisplayChooseProductByUserDTO displayChooseProductByUserDTO , String username) throws Exception {
        //解析token
//        String token = request.getHeader("Authorization");
//        Claims claims = JwtUtil.parseJWT(token);
//        String username = claims.get("username").toString();
        //String form = orderItemService.displayChooseProductsByUser(displayChooseProductByUserDTO, username);
        //调用支付微服务

        //转跳支付界面
        //return Result.ok(form);
        //return aliConfig.goAlipay()
   // }
}
